<?php
namespace Activity\Controllers;

use Activity\Models\Lottery;
use Activity\Models\LotteryLog;
use Activity\Models\Subscribe;
use Commons\App;

class LotteryController extends BaseController
{
    public function initialize()
    {
    }

    public function indexAction()
    {
        //初始化redis方法
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $mPrizeKey = md5('prize_id');
        $ret       = $redis->get($mPrizeKey);
        if (!empty($ret)) {
            $prize_id = json_decode($ret, true);
            $mKey     = md5('get_prize_id' . $prize_id['id']);
            $list     = $redis->lPop($mKey);
            if (!empty($list)) {
                return $this->json([
                    'code'    => 200,
                    'msg'     => '获取成功',
                    'lottery' => [str_replace(substr($list, 3, 4), '****', $list)],
                    'prize'   => $prize_id
                ]);
            } else {
                $redis->delete($mKey);
                $redis->delete($mPrizeKey);
                return $this->json([
                    'code' => 400,
                    'msg'  => '该轮抽奖已经结束',
                ]);
            }
        }
        //先查看数据库奖品数量
        $lottery = new Lottery();
        $prizes  = $lottery->gets();
        if ($prizes == false) {
            return $this->json([
                'code' => 500,
                'msg'  => '抽奖已经结束'
            ]);
        }
        //查询中奖记录表
        $log = new LotteryLog();
        //获取已经抽奖过的人数加入到白名单
        $white = $log->gets();
        if ($white == false) {
            $white = [];
        }
        $whiteList = array_column($white, 'phone', 'uid');
        //获取预约抽奖的人
        $subscribe = new Subscribe();
        $users     = $subscribe->gets();
        $userList  = array_column($users, 'phone', 'userid');

        if (array_sum(array_column($prizes, 'number')) > count($users)) {
            return $this->json([
                'code' => 500,
                'msg'  => '奖品数量大于抽奖人数，需要作出调整'
            ]);
        }
        //生成奖品
        $prize = $prizes[0];
        //保存正在抽奖的奖品
        $redis->set($mPrizeKey, json_encode($prize), 10 * 60);
        //生成能抽奖的人
        $lotteryUser = array_diff($userList, $whiteList);
        if (count($lotteryUser) == 0) {
            return $this->json([
                'code' => 500,
                'msg'  => '活动无名额了'
            ]);
        }
        //生成获奖的人
        $user = array_rand($lotteryUser, $prize['number']);
        if (is_array($user) && count($user) > 0) {
            $data = [];
            foreach ($user as $k => $item) {
                $data[$k] = [
                    'uid'      => $item,
                    'phone'    => $userList[$item],
                    'prize_id' => $prize['id']
                ];
            }
        } else {
            if ($prize['type'] == 1) {
                $data[0] = [
                    'uid'      => $user,
                    'phone'    => '13688176269',
                    'prize_id' => $prize['id']
                ];
            } else {
                $data[0] = [
                    'uid'      => $user,
                    'phone'    => $userList[$user],
                    'prize_id' => $prize['id']
                ];
            }
        }
        $mKey = md5('get_prize_id' . $prize['id']);
        foreach ($data as $v) {
            $redis->rPush($mKey, $v['phone']);
        }
        if (count($data) == 1) {
            $log->add($data[0], $prize['id']);
        } else {
            $log->adds($data, $prize['id']);
        }
        $list = $redis->lPop($mKey);
        return $this->json([
            'code'    => 200,
            'msg'     => '获取成功',
            'lottery' => [str_replace(substr($list, 3, 4), '****', $list)],
            'prize'   => $prize
        ]);
    }

    public function getLotteryPhoneListAction()
    {
        if ($this->request->isPost()) {
            $sub = new Subscribe();
            $ret = $sub->gets();
            $ret = array_column($ret, 'phone');
            if ($ret !== false) {
                return $this->json([
                    'code' => 200,
                    'msg'  => '获取成功',
                    'list' => $ret
                ]);
            }
            return $this->json([
                'code' => 500,
                'msg'  => '获取失败'
            ]);
        }
    }

    public function listAction()
    {
        $log  = new LotteryLog();
        $list = $log->getList();
        if ($list !== false) {
            $prize = array_column($list, 'prize_id');
        } else {
            $prize = [];
        }
        $data = [];
        if ($list !== false) {
            foreach ($list as $k => $value) {
                if (in_array($value['prize_id'], $prize)) {
                    $data[$value['prize_id']][] = [
                        'phone' => $value['phone']
                    ];
                }
            }
        }

        $this->display('lottery/list', ['data' => $data]);
    }

}
